package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;
    //添加新的权限
    @Override
    public void add(Permission permission) {

        permissionDao.add(permission);
    }

    //分页查询
    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        //处理参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);

        Page<Permission> page = permissionDao.findByCondition(queryString);


        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id删除权限项
    @Override
    public Result deleteById(Integer id) {
        //根据id查询是否有关联数据
        Long count = permissionDao.findCountByPermissionId(id);
        if (count > 0){
            return new Result(false, MessageConstant.ASSOCIATED_DATA_DELETE_FAIL);
        }
        permissionDao.deleteById(id);
        return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
    }

    //根据id查询权限项
    @Override
    public Permission findById(Integer id) {

        return permissionDao.findByPermissionId(id);
    }

    //根据id更新权限项
    @Override
    public void editPermission(Permission permission) {

        permissionDao.editPermissionById(permission);
    }

    //查询所有的检查项
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

}
